/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sh.ndt.common;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sh.ndt.exam.entity.TestHistory;
import com.sh.ndt.question.entity.Question;
import com.sh.ndt.user.entity.User;
import com.sh.ndt.user.entity.UserDashboardDetail;
import com.sh.ndt.user.entity.UserEntitlement;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;

/**
 *
 * @author Prashanth
 */
public class ParseJasonObject {

    private static final Logger LOGGER = Logger.getLogger(ParseJasonObject.class);

    static User getUser(String output) {
        LOGGER.debug("Inside getUser Method : " + output);

        Gson gson=  new GsonBuilder().setDateFormat("yyyy-MM-dd").create();

        User user = gson.fromJson(output, User.class);

        return user;
    }

    static UserEntitlement getUserEntitlement(String output) {
        LOGGER.debug("Inside getUserEntitlement Method : " + output);

        Gson gson=  new GsonBuilder().setDateFormat("yyyy-MM-dd").create();

        UserEntitlement userEntitlement = gson.fromJson(output, UserEntitlement.class);

        return userEntitlement;
    }

    static UserDashboardDetail getUserDashboard(String output) {
        LOGGER.debug("Inside getUserDashboard Method : " + output);

        //Gson gson = new Gson();
        Gson gson=  new GsonBuilder().setDateFormat("yyyy-MM-dd").create();

        UserDashboardDetail userDashboardDetail = gson.fromJson(output, UserDashboardDetail.class);

        return userDashboardDetail;
    }

    public static List<User> getUserList(String output) {
        List<User> userList = new ArrayList<User>();

        LOGGER.debug("Inside getUserList Method : " + output);
        try {

            final JSONArray jsa = new JSONArray(output);
            Gson gson=  new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
            
            for (int i = 0; i < jsa.length(); i++) {

                User user = new User();
                user = gson.fromJson(jsa.get(i).toString(), User.class);
//                user.setUserId(jo.getInt("userId"));
//                user.setFirstName(jo.getString("firstName"));
//                user.setLastName(jo.getString("lastName"));
//                user.setPrimaryEmail(jo.getString("primaryEmail"));
//                user.setAddress1(jo.getString("address1"));
//                user.setAddress2(jo.getString("address2"));
//                user.setCity(jo.getString("city"));
//                user.setState(jo.getString("state"));
//                user.setZip(jo.getString("zip"));
//                user.setCountry(jo.getString("country"));
//                user.setCellPhoneNumber(jo.getString("cellPhoneNumber"));
//                user.setOfficePhoneNumber(jo.getString("officePhoneNumber"));
//                user.setSecondaryEmail(jo.getString("secondaryEmail"));
//                //user.setPassword(jo.getString("password"));
//                //user.setConfirmPassword(jo.getString("confirmPassword"));
//                user.setWebSiteAddress(jo.getString("webSiteAddress"));
//                user.setBlogAddress(jo.getString("blogAddress"));
//                user.setLoginCount(jo.getLong("loginCount"));
//                user.setRoleId(jo.getInt("roleId"));
//                user.setUserType(jo.getInt("userType"));
                //Account Month

                userList.add(user);
                LOGGER.debug(user.getFirstName() + " " + user.getLastName());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return userList;
    }

    public static List<Question> getQuestionList(String output) {
        List<Question> questionList = new ArrayList<Question>();

        LOGGER.debug("Inside getQuestionList Method : " + output);
        try {

            final JSONArray jsa = new JSONArray(output);
            Gson gson=  new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
            
            for (int i = 0; i < jsa.length(); i++) {

                Question question;
                question = gson.fromJson(jsa.get(i).toString(), Question.class);
                
                //Account Month
                questionList.add(question);
                LOGGER.debug(question.getQuestionId() + " " + question.getQuestion());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return questionList;
    }

    public static List<TestHistory> getTestHistoryList(String jsOutput) {
        List<TestHistory> testHistoryList = new ArrayList<TestHistory>();

        LOGGER.debug("Inside getTestHistoryList Method : " + jsOutput);
        try {

            final JSONArray jsa = new JSONArray(jsOutput);
            Gson gson=  new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
            
            for (int i = 0; i < jsa.length(); i++) {
                
                TestHistory testHistory;
                testHistory = gson.fromJson(jsa.get(i).toString(), TestHistory.class);

                //Account Month

                testHistoryList.add(testHistory);
                LOGGER.debug(testHistory.getTestHistoryId()+ " " + testHistory.getTestId()+ " " + testHistory.getTestHistoryId());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return testHistoryList;
    }    
}
